#include "common/assert.h"
#include "common/exception.h"
#include "io/stream.h"
#include "io/file_stream.h"

#include <vector>
#include <cstdio>
#include <memory>

void test_stm_basic()
{
	File_Stream stm("Hello.txt");
	printf("Stm is at pos %d\n", stm.pos());
	stm.length(0);
	stm.write("foobar", 6);
	ASSERT(stm.length() == 6);
	stm.length(2);
	ASSERT(stm.length() == 2);
	ASSERT(stm.pos() == 2);
	stm.write("laadilaa", 8);
	ASSERT(stm.length() == 10);
	ASSERT(stm.pos() == 10);
	stm.seek(0);
	std::vector<char> buf(11, '\0');
	stm.read(&buf[0], 10);
	printf("buf is: %s\n", &buf[0]);
	ASSERT(stm.pos() == 10);
}

void test_stm_64bit()
{
	File_Stream stm("large.txt");
	stm.write("raboof", 6);
	stm.length(0x80000000LL);
	ASSERT(stm.length() == 0x80000000LL);
	stm.seek(0x7fffffffLL);

	printf("Writing a 2 gig file, this might take a while.\n");
	stm.write("roofba", 6);
	ASSERT(stm.length() == 0x7fffffffLL + 6);
	ASSERT(stm.pos() == 0x7fffffffLL + 6);

	stm.seek(0x80000002LL);
	ASSERT(stm.pos() == 0x80000002LL);
	std::vector<char> buf(5, '\0');
	stm.read(&buf[0], 3);
	printf("buf is: %s\n", &buf[0]);
	ASSERT(buf[0] == 'f' && buf[1] == 'b' && buf[2] == 'a');
}

int io_test()
{
	try
	{
		test_stm_basic();
		test_stm_64bit();

/*		std::auto_ptr<Stream> stm(new File_Stream("\\\\\\"));

		byte buf[256] = { 0 };

		int file_size = stm->Length();
		int pos = stm->Pos();

		int result = stm->Read(buf, 10);

		printf("Buf: %s\n", buf);
		printf("Got %d bytes.\n", result);*/

	} catch (const Exception& exception)
	{
		std::printf(exception.message());
	}

	getchar();

	return 0;
}